package com.zrwl.poscloud.xo.service.impl;
import com.zrwl.poscloud.base.entity.Grade;
import com.zrwl.poscloud.base.entity.NoticeDept;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.service.GradeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import com.zrwl.poscloud.xo.mapper.NoticeDeptMapper;
import com.zrwl.poscloud.xo.service.NoticeDeptService;
import com.zrwl.poscloud.xo.vo.NoticeDeptVO;

import javax.annotation.Resource;

/**
 * 班级公告 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-03-09
 */
@Service
public class NoticeDeptServiceImpl extends SuperServiceImpl<NoticeDeptMapper,NoticeDept> implements NoticeDeptService {

    @Autowired
    private NoticeDeptService noticeDeptService;
    @Resource
    private NoticeDeptMapper noticeDeptMapper;
    @Autowired
    private GradeService gradeService;
    @Override
    public IPage<NoticeDeptVO> getPageList(NoticeDeptVO noticeDeptVO){
       Page<NoticeDept> page = new Page<>();
       page.setCurrent(noticeDeptVO.getCurrentPage());
       page.setSize(noticeDeptVO.getPageSize());
       IPage<NoticeDeptVO> pageList = noticeDeptMapper.getPageList(page, noticeDeptVO);
       List<NoticeDeptVO> list = pageList.getRecords();
       pageList.setRecords(list);
       return pageList;
}
    @Override
    public List<NoticeDept> getAllList(NoticeDeptVO noticeDeptVO){
        QueryWrapper<NoticeDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<NoticeDept> list = noticeDeptService.list(queryWrapper);
        return list;
}
    @Override
    public String add(NoticeDeptVO noticeDeptVO){
        Grade grade = gradeService.getById(noticeDeptVO.getGradeUid());
        if(grade == null || grade.getStatus() != EStatus.ENABLE){//判断该班级是否存在，若存在是否已毕业
            throw new RuntimeException("该班级不存在或已毕业");
        }
        NoticeDept entity = new NoticeDept();
        BeanUtils.copyProperties(noticeDeptVO, entity, SysConf.STATUS);
        entity.setDeptUid(grade.getDeptUid());
        entity.insert();
        noticeDeptVO.setUid(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);}
    @Override
    public String edit(NoticeDeptVO noticeDeptVO){
        Grade grade = gradeService.getById(noticeDeptVO.getGradeUid());
        if(grade == null || grade.getStatus() != EStatus.ENABLE){//判断该班级是否存在，若存在是否已毕业
            throw new RuntimeException("该班级不存在或已毕业");
        }
        NoticeDept entity = noticeDeptService.getById(noticeDeptVO.getUid());
        BeanUtils.copyProperties(noticeDeptVO, entity);
        entity.setDeptUid(grade.getDeptUid());
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}
    @Override
    public String delete(NoticeDeptVO noticeDeptVO){
        NoticeDept entity = noticeDeptService.getById(noticeDeptVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}
}
